<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="所属楼宇" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="building">
              <j-dict-select-tag type="list" v-model="model.building" dictCode="sxz_building,title,id" placeholder="请选择所属楼宇" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属楼栋" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="build">
              <j-dict-select-tag type="list" v-model="model.build" dictCode="house_build,title,id" placeholder="请选择所属楼栋" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属楼层" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="floor">
              <a-input-number v-model="model.floor" placeholder="请输入所属楼层" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="房号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入房号" ></a-input>
            </a-form-model-item>
          </a-col>
          <!--<a-col :span="12">
            <a-form-model-item label="总面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <a-input-number v-model="model.area" placeholder="请输入面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="可租面积(m²)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableArea">
              <a-input-number v-model="model.enableArea" placeholder="可租面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="租金(元/m²/月)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rent">
              <a-input-number v-model="model.rent" placeholder="请输入最低租金" style="width: 46%" />至
              <a-input-number v-model="model.rentEnd" placeholder="请输入最高租金" style="width: 47%" />
            </a-form-model-item>

          </a-col>
          <!--<a-col :span="12">
            <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tag">
              <a-input v-model="model.tag" placeholder="请输入标签"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="是否办公用房" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isBusiness">
              <j-dict-select-tag type="radio" v-model="model.isBusiness" dictCode="yn" placeholder="请选择是否办公用房" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="可租时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableRent">
              <j-date placeholder="请选择可租时间" v-model="model.enableRent"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否可改造" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableGz">
              <a-input v-model="model.enableGz" placeholder="请输入是否可改造"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物业费(元/m²/月)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wyRent">
              <a-input-number v-model="model.wyRent" placeholder="请输入物业费" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="装修状况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zxStatus">
              <j-dict-select-tag type="list" v-model="model.zxStatus" dictCode="zxType" placeholder="请选择装修状况" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="租售情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="saleStatus">
              <j-dict-select-tag type="list" v-model="model.saleStatus" dictCode="saleStatus" placeholder="请选择租售情况" />
            </a-form-model-item>
          </a-col>
          <!--<a-col :span="12">
            <a-form-model-item label="指导价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zdRent">
              <a-input-number v-model="model.zdRent" placeholder="请输入指导价" style="width: 100%" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="置顶状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isTop">
              <j-dict-select-tag type="list" v-model="model.isTop" dictCode="top_flag" placeholder="请选择置顶状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="房屋类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="houseType">
              <j-dict-select-tag type="list" v-model="model.houseType" dictCode="sxz_house_type,title,id,del_flag = '0'" placeholder="请选择房屋类别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="封面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <j-image-upload  v-model="model.img" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="轮播图" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="banner">
              <j-image-upload isMultiple  v-model="model.banner" ></j-image-upload>
            </a-form-model-item>
          </a-col>
         <!-- <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="introduction">
              <a-textarea v-model="model.introduction" rows="4" placeholder="请输入描述" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="房源描述" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="describes">
              <a-textarea v-model="model.describes" rows="4" placeholder="请输入房源描述" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="详情" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="richText">
              <j-editor v-model="model.richText" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'HouseHomeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //判断是否抽屉页面
      drawer: {
        type: Boolean,
        default: false,
        required: false
      },
      //关联字段
      mainId: {
        type: String,
        default: '',
        required: false
      }
    },
    data () {
      return {
        model:{
          mainId:this.mainId,
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        confirmLoading: false,
        validatorRules: {
          title: [
            { required: true, message: '请输入房号!'},
          ],
         /* area: [
            { required: true, message: '请输入面积!'},
          ],*/
          enableArea: [
            { required: true, message: '请输入可租面积!'},
          ],
/*          rent: [
            { required: true, message: '请输入租金!'},
          ],*/
          saleStatus: [
            { required: true, message: '请选择租售情况!'},
          ],
          isTop: [
            { required: true, message: '请选择置顶状态!'},
          ],
          houseType: [
            { required: true, message: '请选择房屋类型!'},
          ],
          isBusiness: [
            { required: true, message: '请选择是否办公用房!'},
          ],
          zxStatus: [
            { required: true, message: '请选择装修情况!'},
          ],
          img: [
            { required: true, message: '请上传封面!'},
          ],
          banner: [
            { required: true, message: '请上传bannar图!'},
          ],
        },
        url: {
          add: "/houseHome/add",
          edit: "/houseHome/edit",
          queryById: "/houseHome/queryById",
          geturl: "/houseHome/getNewHome",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (buliding,build,floor,wyRent) {
        httpAction(this.url.geturl,{building: buliding,build:build,floor:floor},'post').then((res)=>{
          if (res.success) {
            //this.$message.success(res.message);
            this.model = Object.assign({}, res.result);
            this.$set(this.model,'wyRent',wyRent);
            this.visible = true;
          } else {
            this.$message.warning(res.message);
          }
        });
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        debugger;
        if(that.model.rentEnd&&!that.model.rent){
          that.$message.warning("请填写最低租金");
          return false;
        }
        if(!that.model.rentEnd&&that.model.rent){
          that.$message.warning("请填写最高租金");
          return false;
        }
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (!valid) {
            that.$emit('error');//销毁提交按钮的loading
          }else{
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.$emit('error');//销毁提交按钮的loading
            })
          }
        })
      },
    }
  }
</script>